package com.car.carsharing.service.impl;

import com.car.carsharing.mapper.CarMapper;
import com.car.carsharing.mapper.OrderMapper;
import com.car.carsharing.pojo.Car;
import com.car.carsharing.pojo.Order;
import com.car.carsharing.pojo.UserOrder;
import com.car.carsharing.service.OrderService;
import com.car.carsharing.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CarMapper carMapper;
    @Override
    public int addOrder(Integer userId, Integer carId) {
        Order order = new Order();
        order.setUserId(userId);
        order.setCarId(carId);

        order.setOrderTime(StringUtils.getTime(new Date()));
        int r =  orderMapper.addOrder(order);
        if(r>0){
            Car car = new Car();
            car.setId(carId);
            car.setStatus("1");
            return carMapper.updateCar(car);
        }
        return -1;
    }

    @Override
    public List<UserOrder> getUserOrder(Integer userId) {
        return orderMapper.getUserOrder(userId);
    }

    @Override
    public Integer cancelOrder(Map map) {
        Order order = new Order();
        order.setId((int) map.get("orderId"));
        order.setDelFlag("1");


        if(orderMapper.updateOrder(order) > 0) {
            Car car = new Car();
            car.setId((Integer) map.get("carId"));
            car.setStatus("0");
            carMapper.updateCar(car);
        }

        return -1;
    }
}
